package com.byq.mm.gateway;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


import com.byq.mm.smartlib.component.SensorView;
import com.tc.gateway.GatewayEngine;
import com.tc.gateway.device.ha.TemperatureSensor;
import com.tc.gateway.event.ReportEvent;


public class TemperatureSensorActivity extends Activity{

	private GatewayEngine manager;
	private TemperatureSensor device;
	private BroadcastReceiver messageReceiver;
	private SensorView view;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		AssistanceApplication app = (AssistanceApplication)this.getApplication();
		AndroidHomeApplication ha = app.getHomeApplication();
		manager = ha.getGatewayEngine();
		short node = this.getIntent().getExtras().getShort("node");
		String endpoint = this.getIntent().getExtras().getString("id");
		device = (TemperatureSensor)manager.getDeviceManager().getDevice(endpoint);		 
		view = new SensorView(this, (int)device.getMax(),device.getTemperature(), (short)1, "temperature");
		 
		 setContentView(view);
		messageReceiver = new BroadcastReceiver(){

    		@Override
    		public void onReceive(Context context, Intent intent) {
    	
    			ReportEvent e = (ReportEvent)intent.getExtras().get("event");
    			if(!device.getId().equals(e.getDeviceId()))
    				return;
    			
    			view.update(device.getTemperature(),(int)device.getMax());
    		}
    		
    	};
       
                
                IntentFilter mf = new IntentFilter();
        		mf.addAction(ReportEvent.class.getName());
        		
        		registerReceiver(messageReceiver, mf);	
		
		
	}

	MenuItem receive;
	MenuItem config;
	
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {

			boolean result = super.onCreateOptionsMenu(menu);

			receive = menu.add("����");
			
		
		
			config = menu.add("����");
		
		return result;
	}
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(receive == item){
		
		}
		if(config == item){
			
		}
		return true;
    }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		this.unregisterReceiver(messageReceiver);
		this.messageReceiver = null;
	}
}
